import { ApiProperty } from '@nestjs/swagger';

export class Result<T> {
  @ApiProperty({ description: '是否成功', example: true })
  public readonly success: boolean;
  @ApiProperty({ description: '错误消息提示', example: '失败' })
  public readonly message?: string;
  @ApiProperty({ description: '返回数据', example: {} })
  public readonly data?: T;

  constructor(isSuccess: boolean, message?: string, data?: T) {
    this.success = isSuccess;
    this.message = message;
    this.data = data;
  }

  public static ok<U>(data?: U): Result<U> {
    return new Result<U>(true, undefined, data);
  }

  public static fail<U>(message: string): Result<U> {
    return new Result<U>(false, message);
  }

  public getError(): string {
    return this.message;
  }
}
